package com.example;

import com.alibaba.fastjson.JSONObject;

import javax.persistence.*;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class ExampleMain2 {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        persistPerson(emf);
        /*nativeQuery(emf,"SHOW COLUMNS FROM PERSON");
        nativeQuery(emf,"select * from person");*/
        EntityManager em = emf.createEntityManager();
        String qlString1 = "select p from Person p where p.name = 'Rose'";
        String qlString2 = "select p from Person p where p.salary > 2000l";
        String qlString3 = "select p from Person p where p.department = com.example.Department.IT";
        String qlString4 = "select p from Person p where p.joinDate > '2010-05-01'";
        String qlString5 = "select p from Person p where p.fullTime = true";
        String qlString6 = "select p from Person p where p.salary between 2000 and 4000";
        String qlString7 = "select p from Person p where p.joinDate between :startDate and :endDate order by p.joinDate asc";
        String qlString8 = "select p from Person p where p.salary in :salaries";
        Query query = em.createQuery(qlString8);
        query.setParameter("salaries",Arrays.asList(1500l,3000l));
        List<Person> persons = query.getResultList();
        for (Person person : persons) {
            System.err.println(JSONObject.toJSONString(person,true));
        }
        em.close();

        emf.close();
    }
    private static void persistPerson(EntityManagerFactory emf){
        EntityManager em = emf.createEntityManager();
        Person person1 = new Person("diana",3000l,Department.IT, localDateToDate(1999,11,15),true);
        Person person2 = new Person("Rose",4000l,Department.ADMIN,localDateToDate(2011,5,1),false);
        Person person3 = new Person("Denise",1500l,Department.IT,localDateToDate(2006,1,10),true);
        Person person4 = new Person("Mike",200l,Department.SALE,localDateToDate(2015,8,20),false);
        em.getTransaction().begin();
        em.persist(person1);
        em.persist(person2);
        em.persist(person3);
        em.persist(person4);
        em.getTransaction().commit();
        em.close();
    }
    private static Date localDateToDate(int year,int month,int day){
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.of(year, month, day);
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }
    private static void nativeQuery(EntityManagerFactory emf,String s){
        EntityManager em = emf.createEntityManager();
        Query query = em.createNativeQuery(s);
        List list = query.getResultList();
        for (Object o : list) {
            if (o instanceof Object[]) {
                System.err.println(Arrays.toString((Object[])o));
            }else{
                System.err.println(o);
            }
        }
    }
    private static void findAllPersons(EntityManagerFactory emf){
        EntityManager em = emf.createEntityManager();
        Query query = em.createQuery("SELECT p from Person p");
        List<Person> persons = query.getResultList();
        for (Person person : persons) {
            System.err.println(JSONObject.toJSONString(person,false));
        }
    }
}
